package testCases;

import com.imagotag.test.Conditions;
import com.imagotag.test.TestBuilder;
import com.imagotag.test.results.CsvResultWriter;
import com.imagotag.test.results.TransactionDetailsResultWriter;
import org.joda.time.DateTime;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;

/**
 * @projectName: endurance_test
 * @className: testCases.Application
 * @description:
 * @author: JLONG
 * @date: 2024/10/21 16:49
 * @version: 1.0
 */
public class Application {
    public static void main(String[] args) {
        Application.run();
    }

    public static void run(){
        Application.periodPreloadImageTask();
    }

    public static void periodicTask(){
        TestBuilder testBuilder = Application.initTest("Endurance Test", "-");
        testBuilder.registerTest("Wait for all labels to be online.", test -> test.waitForLabels(TestConfig.labelsWithTags, Conditions.labelsOnline()));  // 测试开始等待价签上线
        Fun_SingleTask.PreCondition(testBuilder, TestConfig.labelsWithTags);
        for (int i = 1; i<1500; i++){
            Fun_SingleTask.singleTask(testBuilder, TestConfig.labelsWithTags, i);
        }
        testBuilder.waitBetweenTests("It's all done", 0, TimeUnit.SECONDS);
        try {
            testBuilder.run(TestConfig.serviceUrl);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void periodPreloadImageTask(){
        TestBuilder testBuilder = Application.initTest("Endurance Test", "preloadImage");
        testBuilder.registerTest("Wait for all labels to be online.", test -> test.waitForLabels(TestConfig.labelsWithTags, Conditions.labelsOnline()));  // 测试开始等待价签上线
        for (int i = 1; i<=300; i++){
            Fun_SingleTask.preloadImage(testBuilder, TestConfig.labelsWithTags, i);
        }
        testBuilder.waitBetweenTests("It's all done", 0, TimeUnit.SECONDS);
        try {
            testBuilder.run(TestConfig.serviceUrl);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static TestBuilder initTest(String testType, String testModule) {
        // 配置测试日志
        DateTime now = DateTime.now();
        String datetime = String.format("%d-%02d-%02d_%02d%02d%02d", now.getYearOfCentury(), now.getMonthOfYear(), now.getDayOfMonth(), now.getHourOfDay(), now.getMinuteOfHour(), now.getSecondOfMinute());
        String csvLogName = testType + "_" + TestConfig.mcu + "_" + TestConfig.FW_Version + "_" + TestConfig.FW_no + "_" + TestConfig.apChannel + "_" + testModule + "_" + ".csv";
        String txtLogName = testType + "_" + TestConfig.mcu + "_" + TestConfig.FW_Version + "_" + TestConfig.FW_no + "_" + TestConfig.apChannel + "_" + testModule + "_" + ".txt";
        File logPath = new File(TestConfig.logsPath, testType + "_" + TestConfig.mcu + "_" + TestConfig.FW_Version + "_" + TestConfig.FW_no + "_" + TestConfig.syncProfile + "_" + TestConfig.encryption + "_" + TestConfig.apChannel + "_" + testModule + "_" + datetime);
        logPath.mkdirs();

        // 创建并配置TestBuilder
        TestBuilder testBuilder = new TestBuilder();
        try {
            testBuilder
                    .writeResults(new CsvResultWriter(new PrintStream(new File(logPath, csvLogName))))    // 标准csv日志输出
                    .writeResults(new TransactionDetailsResultWriter(new PrintStream(new File(logPath, txtLogName))))   // 标准txt日志输出
                    .dontFailOnExistingSetup()
                    .dontFailOnFirstFailedTest()
                    .iterations(1)
                    .setConfiguration("transmissionRetriesCount", "3")  // 任务重试次数，默认3
//                    .disableRetries() // 是否允许任务重试
                    .enableAllTasks()
//                    .enableEncryption("imagotag") //设置 encryption key
                    .disableBackgroundTasks()
                    .setConfiguration("lancomDiscoveryEnabled", "false")
                    .setConfiguration("ignoreArchiveTasksForUpdateStatusQueries", "false")
                    .setConfiguration("syncProfile", TestConfig.syncProfile); // NORMAL（NORMAL; SUPER_FAST_RESPONSE; ULTRA_FAST_RESPONSE）
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return testBuilder;
    }
}
